<?php

/**
 * Created by PhpStorm.
 * User: hongyeah
 * Date: 2015/10/23
 * Time: 12:53
 */
namespace Home\Model;

use Think\Model;

class UsersModel extends Model
{
    public function edtUsers($data){
        $exist = $this->where(array('cardnum' => $data['cardnum']))->find();
        if(empty($exist)){
            return false;
        }

        $this->where(array('cardnum' => $data['cardnum']))->save($data);

        return true;
    }

    public function edtUsersByID($data){
        $exist = $this->where(array('person_id' => $data['person_id']))->find();
        if(empty($exist)){
            return false;
        }

        $this->where(array('person_id' => $data['person_id']))->save($data);

        return true;
    }

    public function checkPassWord($where){
        return $this->where($where)->count();
    }

    public function edtUserPassWord($data){
        $result = $this->where(array('person_id' => $data['person_id']))->save($data);
    }

    public function getUsersByNum($cardnum){
        $data = $this->where(array('cardnum'=>$cardnum))->find();
        return $data;
    }
    public function getUsersByID($id){
        $data = $this->field('cardnum as number, name, department,major,sex,address,phone')->where(array('person_id'=>$id))->select();
        return $data;
    }

    public function arrangeRegs($data){
        $result = M('record')->data($data)->add();
        if($result==false) return false;
        else return true;
    }

    public function getPersonRecord($where){
        $result = M('record')->where($where)->find();
        if($result==null) return false;
        return true;
    }

    public function getRank($where){
        $result = M('record')->field('max(rank)')->where($where)->find();
        if($result['max(rank)']==null) return 0;
        else return $result['max(rank)'];
    }

    public function getAllRecord($where){
        return M('record')->where($where)->order('see_date desc')->select();

    }
    public function getDoctors(){
        $today = date('w'); //获取当前星期几
        $tomorrow = ($today+1)%7;
        $todaytomorrow = ($tomorrow+1)%7;
        $doctors = array();
        $this->selectDoctors($doctors,0,$today,1);
        $this->selectDoctors($doctors,count($doctors),$tomorrow,2);
        $this->selectDoctors($doctors,count($doctors),$todaytomorrow,3);
        return $doctors;
    }

    public function selectDoctors(&$doctors,$k,$day,$type){
        $schedule_model = M('schedule');
        switch($day){
            case 1 : $result = $schedule_model->field('dot_id,Mon as day')->select();break;
            case 2 : $result = $schedule_model->field('dot_id,Tue as day')->select();break;
            case 3 : $result = $schedule_model->field('dot_id,Wed as day')->select();break;
            case 4 : $result = $schedule_model->field('dot_id,Thu as day')->select();break;
            case 5 : $result = $schedule_model->field('dot_id,Fri as day')->select();break;
            case 6 : $result = $schedule_model->field('dot_id,Sat as day')->select();break;
            case 0 : $result = $schedule_model->field('dot_id,Sun as day')->select();break;
        }

        for($i=0;$i<count($result);$i++){
            if($result[$i]['day']>0){
                $doctors[$k]['id']=$result[$i]['dot_id'];
                $data = M('doctors')->where(array('dot_id'=>$result[$i]['dot_id']))->find();
                $doctors[$k]['name'] = $data['dot_name'] ;
                $doctors[$k]['flag'] = $type;
                if($data['dot_major']=='内科'){
                    $doctors[$k++]['major'] = 1;
                }else{
                    $doctors[$k++]['major'] = 2;

                }
            }
        }

    }

    public function login($cardnum,$password){
        $data = new \stdClass();
        $where['cardnum'] = $cardnum;
        $where['password'] = $password;
        $data->messages = $this->where($where)->select();
        $data->doctors = $this->getDoctors();
        return $data;
    }

}